﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

public partial class FeedSubscription : ICPAdminPage
{
    public DataTable m_dtSource = new DataTable();

    #region Properties
    private int m_iUserRoleId
    {
        get { 
            int iRet = -1;
            try
            {
                iRet = Convert.ToInt32(Session[KSession.FeedSubscription.USER_ROLE_ID]);
            }
            catch { /* LET IT BE MINUS */}
            return iRet;
        }
        set {
            Session[KSession.FeedSubscription.USER_ROLE_ID] = value;
        }
    }
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Pengaturan Feed Source";
        if (!IsPostBack && !IsCallback)
        {
            m_iUserRoleId = -1;
        }

        InitGrid(false);
    }
    
    protected void dgMain_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        Handle_Grid(e.Parameters);
    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitGrid(bool bRefresh)
    {
        if (m_iUserRoleId >= 0)
        {
            Cached.FeedSubscription ccTmp = new Cached.FeedSubscription();
            m_dtSource = ccTmp.GetFeedSubscriptionList(m_iUserRoleId, bRefresh);

            dgMain.Caption = "ICP Feed Subscription by User Role";

            dgMain.DataSource = m_dtSource;
            
            if (bRefresh)
            {
                for (int i = 0; i < m_dtSource.Rows.Count; i++)
                {
                    dgMain.Selection.SetSelectionByKey(
                        m_dtSource.Rows[i]["SRC_ID"],
                        m_dtSource.Rows[i]["IS_CHECKED"].ToString() == KGlobal.YES);
                }
            }

            dgMain.DataBind();
        }
    }

    private String Handle_Grid(String sxParameter)
    {
        String sxReturn = String.Empty;
        String[] sxSplitted = sxParameter.Split(new String[] {KGlobal.DELIMITER}, StringSplitOptions.None);

        switch (sxSplitted[0].ToUpper())
        {
            case "INIT":
                m_iUserRoleId = int.Parse(sxSplitted[1]);
                InitGrid(true);
                break;
            case "SAVE":
                UpdateFeedSubscription();
                break;
        }
        return sxReturn;
    }

    private void UpdateFeedSubscription()
    {
        Cached.FeedSubscription ccTmp = new Cached.FeedSubscription();
        DataTable dtOld = ccTmp.GetFeedSubscriptionList(m_iUserRoleId, true);
        DataTable dtUpdate = (DataTable)dgMain.DataSource;

        DBLib dbl = new DBLib();

        dbl.BeginTransaction();
        try
        {
            for (int i = 0; i < dtOld.Rows.Count; i++)
            {
                bool IsOldSelected = dtOld.Rows[i]["IS_CHECKED"].ToString() == KGlobal.YES;
                int iFeedSourceId = dtOld.Rows[i].ReadInt("SRC_ID");
                bool IsNewSelected = dgMain.Selection.IsRowSelectedByKey(dtOld.Rows[i]["SRC_ID"]);

                if (IsOldSelected && !IsNewSelected)
                {
                    DBQuery.FeedSubscriptionTable.DeleteFeedSubscription(dbl, iFeedSourceId, m_iUserRoleId);
                }
                else if (!IsOldSelected && IsNewSelected)
                {
                    DBQuery.FeedSubscriptionTable.AddFeedSubscription(dbl, iFeedSourceId, m_iUserRoleId);
                }
            }

            dbl.EndTransaction(true);

            InitGrid(true);
        }
        catch (Exception ex)
        {
            dbl.EndTransaction(false);
            throw ex;
        }
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods

    
}
